package org.springsource.controllers;

import java.security.Principal;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;
import javax.inject.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.app.ItemDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springsource.domain.Person;
import org.springsource.domain.Player;
import org.springsource.domain.Team;
import org.springsource.service.AccountService;
import org.springsource.service.CountryService;
import org.springsource.service.PersonService;
import org.springsource.service.PlayerService;
import org.springsource.service.TeamService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	private final Provider<ConnectionRepository> connectionRepositoryProvider;
	@Autowired ItemDao item;
	
	@Autowired
	private AccountService accountService; 
	
	@Autowired
	private CountryService countryService; 
	
	@Autowired
	private TeamService teamService;
	
	@Autowired
	private PlayerService playerService;
	
	@Autowired
	private PersonService personService;
	
	@Inject
	public HomeController(Provider<ConnectionRepository> connectionRepositoryProvider) {
		this.connectionRepositoryProvider = connectionRepositoryProvider;
	}
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Principal currentUser, Model model) {
		logger.info("Welcome home! the client locale is ");
		model.addAttribute("connectionsToProviders", getConnectionRepository().findAllConnections());
		model.addAttribute(accountService.getAccount(currentUser.getName()));
		List<Team> engTeams = teamService.getPremierLeagueTeams();
		List<Team> spanishTeams = teamService.getPrimeraDivisionTeams();
		
		for(Team team : engTeams){
			logger.info("team: "+team.getName());
		}
		model.addAttribute("engTeams", engTeams);
		model.addAttribute("spaTeams", spanishTeams);
		PasswordEncoder enc = new StandardPasswordEncoder("ot1do6");
		logger.info(enc.encode("ot1do6"));
		return "home";
	}
	@RequestMapping(value = "/person", method = RequestMethod.GET)
	public @ResponseBody List<Person> person(Principal currentUser, Model model) {
		return personService.getAll();
		
	}
	@RequestMapping(value = "/test", method = RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Player test(Locale locale, Model model) {
		logger.info("Welcome home! test "+ locale.toString());
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		
		Team levski = teamService.getTeam(new Long(1));
		logger.info("players size:"+levski.getPlayers().size());
		//logger.info(levski.getName());
		//logger.info("players size:"+levski.getPlayers().size());
		Player plamen = playerService.getPlayer(new Long(436702610));
		logger.info(plamen.getFirstName());
		return plamen;
	}
	private ConnectionRepository getConnectionRepository() {
		return connectionRepositoryProvider.get();
	}
	
}
